<?php
#Specifying an Array Not Beginning at Element 0

$presidents = array(1=>"Wasjomgtpon", "Adams", "Jefferson", "madison");

foreach ($presidents as $number => $president){
    print "$number: $president" . "<br/>";
}
echo "<br/>";

$reconstruction_presidents = array(16 => 'Lincoln', 'Johnson', 'Grant');
foreach ($reconstruction_presidents as $number => $preconstruction_presidents){
    print "$number: $preconstruction_presidents" . "<br/>";
}
echo "<br/>";

$us_leaders = array(-1 => 'George II', 'George III', 'Washington');
foreach ($us_leaders as $number => $us_leader){
    print "$number: $us_leader" . "<br/>";
}
echo "<br/>";

#Storing Multiple Elements per Key in an Array

$fruits = array(); 
$fruits['red'][] = 'strawberry'; 
$fruits['red'][] = 'apple'; 
$fruits['yellow'][] = 'banana';

print_r($fruits);
echo "<br/>";

#while (list($color,$fruit) = mysqli_fetch_assoc($r)) { $fruits[$color][] = $fruit;}

foreach ($fruits as $color => $color_fruit) { 
// $color_fruit is an array
foreach ($color_fruit as $fruit) {
print "$fruit is colored $color.<br>"; }
}
echo "<br/>";

$cards = range(1, 52);

$odd = range(1, 52, 2);

#Iterating Through an Array
#foreach ($array as $value) { // Act on $value}
#foreach ($array as $key => $value) { // Act II }
#reset($array); // reset internal pointer to beginning of array while (list($key, $value) = each ($array)) {
// Final Act }

$presidents = array(1=>"Wasjomgtpon", "Adams", "Jefferson", "madison");

reset($presidents);

while (list($number, $president) = each ($presidents)) {
    print "$number: $president" . "<br/>";
}
echo "<br/>";

if (is_array($items)) {
// foreach loop code for array
}else{
// code for scalar
}
echo "<br/>";

#Deleting Elements from an Array

#unset($array[3]);
#unset($array['foo']);

#To delete multiple noncontiguous elements, also use unset():
#unset($array[3], $array[5]);
#unset($array['foo'], $array['bar']);

// create a "numeric" array
$animals = array('ant', 'bee', 'cat', 'dog', 'elk', 'fox'); array_splice($animals, 2, 2);
print_r($animals);
echo "<br/>";

#4.6 Changing Array Size
// start at three
$array = array('apple', 'banana', 'coconut'); // grow to five
$array = array_pad($array, 5, '');
print count($array);
echo "<br/>";

// no assignment to $array
array_splice($array, 2);
print count($array);
echo "<br/>";

#Appending One Array to Another
$p_languages = array('Perl', 'PHP');
$p_languages = array_merge($p_languages, array('Python')); 
print_r($p_languages);
echo "<br/>";

#Checking if a Key Is in an Array
#if (array_key_exists('key', $array)) {
/* there is a value for $array['key'] */
#}
#if (isset($array['key'])) { /* there is a non-null value for 'key' in $array */ }

#4.11 Checking if an Element Is in an Array
#if (in_array($value, $array)) {
// an element has $value as its value in array $array
#}

#Finding the Position of a Value in an Array

echo "<br/>";
$favorite_foods = array(1=> "artichokes", "bread", "cauliflower", "deviled eggs");
$food = "cauliflower";
$position = array_search($food, $favorite_foods);

if ($position !== false) {
    echo "my $position favorite food is $food";
} else {
    echo "Blech! I have $food";
}














?>
